﻿
using System;
using System.Data;
using System.Collections.Generic;
using BYFW.SFL;
using BYFW.BOL;

namespace LDSP.BRL
{
    public partial class DownloadLog : BYFW.BOL.BaseEntity
    {
        public DownloadLog()
        {
            m_tableName = "t_downloadLog";
            m_tblCacheManager.IsEnable = true;
        }

        protected int m_docID = 0;
        protected string m_docName = string.Empty;
        protected DateTime m_downloadDT = DateTime.Parse("1970-01-01");
        protected int m_enterpriseID = 0;
        protected string m_enterpriseCode = string.Empty;
        protected string m_fileName = string.Empty;
        protected DocType m_docType = DocType.法律法规;
        protected int m_enterpriseUserID = 0;
        protected string m_enterpriseUserName = string.Empty;
        protected Enterprise m_enterprise = null;
        protected Codification m_codification = null;
        protected EnterpriseUser m_enterpriseUser = null;

        /// <summary>
        /// 文档ID
        /// </summary>
        public int DocID
        {
            get { return m_docID; }
            set { m_docID = value; }
        }
        /// <summary>
        /// 文档名
        /// </summary>
        public string DocName
        {
            get
            {
                return m_docName;
            }
            set { m_docName = value; }
        }
        /// <summary>
        /// 下载时间
        /// </summary>
        public DateTime DownloadDT
        {
            get { return m_downloadDT; }
            set { m_downloadDT = value; }
        }
        /// <summary>
        /// 企业ID
        /// </summary>
        public int EnterpriseID
        {
            get { return m_enterpriseID; }
            set { m_enterpriseID = value; }
        }
        /// <summary>
        /// 企业名
        /// </summary>
        public string EnterpriseCode
        {
            get { return m_enterpriseCode; }
            set { m_enterpriseCode = value; }
        }
        /// <summary>
        /// 下载的文件名
        /// </summary>
        public string FileName
        {
            get { return m_fileName; }
            set { m_fileName = value; }
        }
        /// <summary>
        /// 文档类型
        /// </summary>
        public DocType DocType
        {
            get { return m_docType; }
            set { m_docType = value; }
        }
        /// <summary>
        /// 企业用户ID
        /// </summary>
        public int EnterpriseUserID
        {
            get { return m_enterpriseUserID; }
            set { m_enterpriseUserID = value; }
        }
        /// <summary>
        /// 企业用户名称
        /// </summary>
        public string EnterpriseUserName
        {
            get { return m_enterpriseUserName; }
            set { m_enterpriseUserName = value; }
        }

        public Enterprise Enterprise
        {
            get
            {
                if (m_enterprise == null)
                {
                    m_enterprise = new Enterprise();
                    if (m_enterpriseID != 0)
                    {
                        m_enterprise.GetProperties(m_enterpriseID);
                    }
                }
                return m_enterprise;
            }
        }
        public EnterpriseUser EnterpriseUser
        {
            get
            {
                if (m_enterpriseUser == null)
                {
                    m_enterpriseUser = new EnterpriseUser();
                    if (m_enterpriseUserID != 0)
                    {
                        m_enterpriseUser.GetProperties(m_enterpriseUserID);
                    }
                }
                return m_enterpriseUser;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("docID", m_docID);
            m_container.Add("docName", m_docName);
            m_container.Add("downloadDT", m_downloadDT);
            m_container.Add("enterpriseID", m_enterpriseID);
            m_container.Add("enterpriseCode", m_enterpriseCode);
            m_container.Add("fileName", m_fileName);
            m_container.Add("docType", (int)m_docType);
            m_container.Add("enterpriseUserID", m_enterpriseUserID);
            m_container.Add("enterpriseUserName", m_enterpriseUserName);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_docID = this.GetIntValue(record, "docID");
            m_docName = this.GetStringValue(record, "docName");
            m_downloadDT = this.GetDateValue(record, "downloadDT");
            m_enterpriseID = this.GetIntValue(record, "enterpriseID");
            m_enterpriseCode = this.GetStringValue(record, "enterpriseCode");
            m_fileName = this.GetStringValue(record, "fileName");
            m_docType = (DocType)this.GetIntValue(record, "docType");
            m_enterpriseUserID = this.GetIntValue(record, "enterpriseUserID");
            m_enterpriseUserName = this.GetStringValue(record, "enterpriseUserName");
            return true;
        }

        /// <summary>
        /// 创建新的下载记录，同时企业的下载计数加1，若不允许下载，则返回值表示拒绝理由，允许下载，则返回值为空字符串
        /// </summary>
        /// <param name="loginUser"></param>
        /// <param name="docID"></param>
        /// <param name="docType"></param>
        /// <returns></returns>
        public string CreateNew(EnterpriseUser loginUser, int docID, DocType docType)
        {
            if (loginUser == null)
            {
                return "请登录后下载！";
            }
            DownloadLog ett = new DownloadLog();
            ett.EnterpriseID = loginUser.EnterpriseID;
            ett.EnterpriseUserID = loginUser.ID;
            ett.EnterpriseUserName = loginUser.Name;
            ett.EnterpriseCode = ett.Enterprise.Code;
            ett.DownloadDT = DateTime.Now.Date;
            ett.DocID = docID;
            ett.DocType = docType;
            if (docType == BRL.DocType.标准 || docType == BRL.DocType.法律法规)
            {
                Codification tmp = new BRL.Codification();
                tmp.GetProperties(docID);
                ett.DocName = tmp.Name;
                ett.FileName = tmp.FileName;
                //自己公司的文档：允许下载，且不记录
                if (ett.EnterpriseID == tmp.EnterpriseID)
                {
                    return "";
                }
                else if (tmp.EnterpriseID != 0)
                {
                    return "其他企业的文档，您没有权限下载！";
                }
            }
            else if (docType == BRL.DocType.事故案例)
            {
                AccidentCase tmp = new BRL.AccidentCase();
                tmp.GetProperties(docID);
                ett.DocName = tmp.Name;
                ett.FileName = tmp.FileName;
            }
            else if (docType == BRL.DocType.新闻)
            {
                News tmp = new BRL.News();
                tmp.GetProperties(docID);
                ett.DocName = tmp.Name;
                ett.FileName = tmp.FileName;
            }
            else if (docType == BRL.DocType.征求意见稿)
            {
                ExposureDraft tmp = new BRL.ExposureDraft();
                tmp.GetProperties(docID);
                ett.DocName = tmp.Name;
                ett.FileName = tmp.FileName;
            }

            string cond = string.Format("where {0}={1} and {2}={3} and {4}={5}",
                "enterpriseID", ett.EnterpriseID,
                "docID", ett.DocID,
                "docType", (int)ett.DocType);
            //下载过：允许下载，且不记录
            if (this.Exist(null, cond, false))
            {
                return "";
            }
            //未下载过，未达到最大下载次数：允许下载，且记录
            if (CommonOP.GetInt(SystemConfig.MaxDownloadTimes, true) > ett.Enterprise.DownloadTimes)
            {
                ett.Add();
                ett.Enterprise.DownloadTimes = ett.Enterprise.DownloadTimes + 1;
                ett.Enterprise.TotalDownloadTimes = ett.Enterprise.TotalDownloadTimes + 1;
                ett.Enterprise.Update();
                return "";
            }
            else
            {
                return "已达到最大下载次数，请与系统管理员联系！";
            }
        }

        public DataTable GetRecordsByCond(string cond)
        {
            string sql = @"select * from (select t4.*,t2.name as dName, t3.Code as eName from t_downloadLog t4
                left join t_codification t2 on t4.docID=t2.id
                left join t_enterprise t3 on t4.enterpriseID=t3.id) t1";
            sql += cond;
            return DB.Select(sql).Tables[0];
        }

        public void UpdateByDocName(int docID, DocType docType, string cofiditionName)
        {
            string sql = string.Format("update {0} set {1}={2} where {3}={4} and {5}={6}",
                m_tableName,
                DB.GetCol("docName"), DB.GetVal(cofiditionName),
                DB.GetCol("docID"), DB.GetVal(docID),
                DB.GetCol("docType"), DB.GetVal((int)docType));
            m_tblCacheManager.Clear();
            m_objCacheManager.Clear();
            DB.ExecuteNonQuery(sql);
        }

        public void UpdateByEnterprise(int enterpriseID, string enterpriseCode)
        {
            string sql = string.Format("update {0} set {1}={2} where {3}={4}",
               m_tableName,
               DB.GetCol("enterpriseCode"), DB.GetVal(enterpriseCode),
               DB.GetCol("enterpriseID"), DB.GetVal(enterpriseID));
            m_tblCacheManager.Clear();
            m_objCacheManager.Clear();
            DB.ExecuteNonQuery(sql);
        }

        public void UpdateByEnterpriseUser(int enterpriseUserID, string enterpriseUserName)
        {
            string sql = string.Format("update {0} set {1}={2} where {3}={4}",
                   m_tableName,
                   DB.GetCol("enterpriseUserName"), DB.GetVal(enterpriseUserName),
                   DB.GetCol("enterpriseUserID"), DB.GetVal(enterpriseUserID));
            m_tblCacheManager.Clear();
            m_objCacheManager.Clear();
            DB.ExecuteNonQuery(sql);
        }

        public DataTable GetReportForEnterprise(int topNumber, string startDT, string endDT)
        {
            string sql = string.Format("select top {0} count(enterpriseCode) as times, enterpriseCode from (select * from t_downloadLog where downloadDT >= '{1}' and downloadDT <='{2}') t1 group by enterpriseCode order by COUNT(enterpriseCode) desc",
                topNumber,
                startDT,
                endDT);
            DataTable dt = DB.Select(sql).Tables[0];
            return dt;
        }

        public DataTable GetReportForDoc(int topNumber, string startDT, string endDT)
        {
            string sql = string.Format("select top {0} count(docName) as times, docName from (select * from t_downloadLog where downloadDT >= '{1}' and downloadDT <='{2}') t1 group by docName order by COUNT(docName) desc",
                topNumber,
                startDT,
                endDT);
            DataTable dt = DB.Select(sql).Tables[0];
            return dt;
        }

        public DataTable GetReportForFavorateDoc(int topNumber, string startDT, string endDT, int enterpriseID)
        {
            string sql = string.Format("select top {0} count(docName) as times, docName from (select * from t_downloadLog where downloadDT >= '{1}' and downloadDT <='{2}' and enterpriseID = {3}) t1 group by docName order by COUNT(docName) desc",
                topNumber,
                startDT,
                endDT,
                enterpriseID);
            DataTable dt = DB.Select(sql).Tables[0];
            return dt;
        }
    }
}
